using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.IO;
namespace Orbifold.Trefoil
{
    /// <summary>
    /// Assuming that a shape is inheriting from Control this tool looks for a TextBlock with the name
    /// 'BodyText' in the template and sets the FontStyle to Italic. 
    /// </summary>
    class MakeItalicTool : ToolBase
    {
        /// <summary>
        /// Function to execute the command.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnExecute(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {           
            if (SelectionManager.SelectedText != null)
            {
             
                 //make it undoable
                MakeItalicCommand cmd = new MakeItalicCommand(SelectionManager.SelectedText);
                UndoManager.Execute(cmd);               
            }
        }

        /// <summary>
        /// Determines if a command is enabled. Override to provide custom behavior. Do not call the
        /// base version when overriding.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnQueryEnabled(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {

            e.CanExecute= (SelectionManager.SelectedText.Text.Length>0);
            e.Handled = true;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MakeItalicTool"/> class.
        /// </summary>
        public MakeItalicTool()
            : base("Make italic")
        {

        }

        #region ItalicCommand
        class MakeItalicCommand : ICommand
        {
            #region Fields
            private TextRange mText;
            //private MemoryStream memory;
            private TextPointer ps, pe;
            #endregion

            #region Constructor
            ///<summary>
            ///Default constructor
            ///</summary>
            public MakeItalicCommand(TextRange text)
            {
                mText = text;
                ps = mText.Start;
                pe = mText.End;
                //memory = new MemoryStream();
                //mText.Save(memory, System.Windows.DataFormats.Xaml);
            }
            #endregion


            #region ICommand Members

            public void Undo()
            {
                FlipItalic();
            }

            public void Redo()
            {

                FlipItalic();
            }

            private void FlipItalic()
            {
                //mText.Load(memory, System.Windows.DataFormats.Xaml);
                SelectionManager.TextEditor.Selection.Select(ps,pe);
                if (mText.GetPropertyValue(RichTextBox.FontStyleProperty) == DependencyProperty.UnsetValue)//happens when the selection is mixed
                {
                    mText.ApplyPropertyValue(RichTextBox.FontStyleProperty, "Italic");
                    return;
                }
                else
                {
                    FontStyle o = (FontStyle)mText.GetPropertyValue(RichTextBox.FontStyleProperty);
                    if (o == null) return;
                    if (o == FontStyles.Italic)
                        mText.ApplyPropertyValue(RichTextBox.FontStyleProperty, "Normal");
                    else
                        mText.ApplyPropertyValue(RichTextBox.FontStyleProperty, "Italic");
                }
            }
            public string Text
            {
                get
                {
                    return "Italic text";
                }
                set
                {
                    throw new Exception("The method or operation is not implemented.");
                }
            }

            #endregion
        }
        #endregion
    }
    
}
